
//处理
define(["core","core.service","angular.util","angular"], function(core,service,util)
{
	
	var handler={};
	
	var match=function(handlerArray)
	{
		for(var i=0,len=handlerArray.length;i<len;i++)
		{
			if(handlerArray[i].isHandle())
			{
				handlerArray[i].handle();
				break;
			}
		}
	};
	
	//软件
	handler.software=function(software,module)
	{
		var handlerArray=
		[
			//设置推荐
			{
				isHandle:function()
				{
					return (module.id==22);
				},
				handle:function()
				{
					core.ui.simpleDialogEx("是否设置/取消推荐?",function()
					{
						service.software.setRecommend(software.id,function(result)
						{
							core.ui.simpleDialog(result.message);
						});
					});
				}
			},
			//编辑
			{
				isHandle:function()
				{
					return (module.id==23 || module.id==24);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"编辑软件",
						path:util.getIncludePath("/software_edit.html"),
						controllerName:"SoftwareEditController"
					},function($editScope,dialog)
					{
						$editScope.software=software;
						
						$editScope.save=function()
						{
							core.ui.simpleDialogEx("编辑后会重新审核,是否继续?",function()
							{
								service.software.edit($editScope.software,function(result)
								{
									dialog.close();
									if(result.success)
										result.message=core.message.message.UPDATE_SUCCESS;
									else
										result=core.message.parseResult(parse);
									core.ui.simpleDialog(result.message,function(result)
									{
										if(result.success)
											util.refresh();
									});
								});
							});
						};
						
						$editScope.cancel=function()
						{
							dialog.close();
						};
					});
				}
			}
		];
		
		match(handlerArray);
	};
	
	//讨论
	handler.discuss=function(discuss,module)
	{
		var handlerArray=
		[
			//编辑
			{
				isHandle:function()
				{
					return (module.id==25 || module.id==26);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"编辑讨论",
						path:util.getIncludePath("/discuss_edit.html"),
						controllerName:"DiscussEditController"
					},function($editScope,dialog)
					{
						$editScope.discuss=discuss;
						
						$editScope.save=function()
						{
							service.discuss.edit($editScope.discuss,function(result)
							{
								dialog.close();
								if(result.success)
									result.message=core.message.message.UPDATE_SUCCESS;
								else
									result=core.message.parseResult(parse);
								
								core.ui.simpleDialog(result.message,function()
								{
									if(result.success)
										util.refresh();
								});
							});
						};
						
						$editScope.cancel=function()
						{
							dialog.close();
						};
					});
				}
			},
			//删除
			{
				isHandle:function()
				{
					return (module.id==27 || module.id==28);
				},
				handle:function()
				{
					core.ui.simpleDialogEx("是否删除?",function()
					{
						service.discuss.remove(discuss.id,function(result)
						{
							core.ui.simpleDialog(result.message,function()
							{
								if(result.success)
									util.changeLocation();
							});
						});
					});
				}
			},
			//移动板块
			{
				isHandle:function()
				{
					return (module.id==29);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"移动板块",
						path:util.getIncludePath("/discuss_moveForum.html"),
						controllerName:"DiscussMoveForumController"
					},function($editScope,dialog)
					{
						$editScope.discuss=discuss;
						
						service.discuss.forum.find(function(data)
						{
							$editScope.forums=data;
							$editScope.$apply();
						});
						
						$editScope.save=function()
						{
							service.discuss.moveForum(discuss.id,discuss.forum.id,function(result)
							{
								dialog.close();
								core.ui.simpleDialog(result.message,function()
								{
									if(result.success)
										util.refresh();
								});
							});
						};
					});
				}
			},
			//设置排序
			{
				isHandle:function()
				{
					return (module.id==36);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"更新排序",
						path:util.getIncludePath("/discuss_updateOrder.html"),
						controllerName:"DiscussUpdateOrderController"
					},function($editScope,dialog)
					{
						service.discuss.order.find(function(data)
						{
							if(discuss.order.id==0)
								discuss.order=data[0];
							
							$editScope.discuss=discuss;
							$editScope.orders=data;
							$editScope.$apply();
						});
						
						$editScope.save=function()
						{
							service.discuss.updateOrder(discuss.id,discuss.order.id,function(result)
							{
								dialog.close();
								core.ui.simpleDialog(result.message);
							});
						};
					});
				}
			},
			{
				isHandle:function()
				{
					return (module.id==55);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"举报原因",
						path:util.getIncludePath("/report_create.html"),
						controllerName:"ReportCreateController"
					},function($createScope,dialog)
					{
						$createScope.save=function()
						{
							var report=$createScope.report;
							report.id=discuss.id;
							service.discuss.report.create(report,function(result)
							{
								dialog.close();
								core.ui.simpleDialog(result.message,function()
								{
									if(result.success)
										util.changeLocation();
								});
							});	
						}
					});
				}
			}
		];
		
		match(handlerArray);
	};
	
	//动弹
	handler.dongtan=function(dongtan,module)
	{
		var handlerArray=
		[
			//编辑
			{
				isHandle:function()
				{
					return (module.id==30 || module.id==31);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"编辑动弹",
						path:util.getIncludePath("/dongtan_edit.html"),
						controllerName:"DongtanEditController"
					},function($editScope,dialog)
					{
						$editScope.dongtan=dongtan;
						
						$editScope.save=function()
						{
							service.dongtan.edit($editScope.dongtan,function(result)
							{
								dialog.close();
								core.ui.simpleDialog(result.message,function()
								{
									if(result.success)
										util.refresh();
								});
							});
						};
					});
				}
			},
			//删除
			{
				isHandle:function()
				{
					return (module.id==32 || module.id==33);
				},
				handle:function()
				{
					core.ui.simpleDialogEx("是否删除?",function()
					{
						service.dongtan.remove(dongtan.id,function(result)
						{
							core.ui.simpleDialog(result.message,function()
							{
								if(result.success)
									util.changeLocation();
							});
						});
					});
				}
			}
		];
		match(handlerArray);
	};
	
	handler.user=
	{
		follow:
		{
			//关注
			create:function(user)
			{
				core.ui.simpleDialogEx("是否关注?",function()
				{
					service.user.follow.create(user.id,function(result)
					{
						core.ui.simpleDialog(result.message);
					});
				});
			},
			//取消关注
			clear:function(user)
			{
				core.ui.simpleDialogEx("是否取消关注?",function()
				{
					service.user.follow.clear(user.id,function(result)
					{
						core.ui.simpleDialog(result.message);
					});
				});
			}
		},
		mail:
		{
			create:function(user)
			{
				var title="发送邮件";
				if(!core.util.isBlank(user))
					title="给"+user.username+title;
				
				util.angular.dialog.baseDialog
				({
					title:title,
					path:util.getIncludePath("/user_mail_create.html"),
					controllerName:"MailCreateController"
				},function($create$scope,dialog)
				{
					if(!core.util.isBlank(user))
						$create$scope.mail={target:user.username};
					
					$create$scope.save=function()
					{
						service.user.mail.create($create$scope.mail,function(result)
						{
							if(result.success)
							{
								result.message=core.message.message.MAIL_CREATE_SUCCESS;
								dialog.close();
							}
							
							core.ui.simpleDialog(result.message);
						});
					};
				});
			},
			remove:function(mail,callback)
			{
				core.ui.simpleDialogEx("是否删除?",function()
				{
					service.user.mail.remove(mail.id,function(result)
					{
						core.ui.simpleDialog(result.message);
						callback(result);
					});
				});
			},
			setRead:function(mail,callback)
			{
				core.ui.simpleDialogEx("是否设置已读?",function()
				{
					service.user.mail.setRead(mail.id,function(result)
					{
						core.ui.simpleDialog(result.message);
						callback(result);
					});
				});
			}
		}
	};
	
	//博客
	handler.blog=function(blog,module)
	{
		var handlerArray=
		[
			//自荐
			{
				isHandle:function()
				{
					return (module.id==61);
				},
				handle:function()
				{
					core.ui.simpleDialogEx("是否自荐?",function()
					{
						service.blog.recommend.create(blog.id,function(result)
						{
							if(result.success)
								result.message=core.message.message.CREATE_SUCCESS;
							
							core.ui.simpleDialog(result.message);
						});
					});
				}
			},
			//推荐
			{
				isHandle:function()
				{
					return (module.id==40);
				},
				handle:function()
				{
					core.ui.simpleDialogEx("是否设置/取消推荐?",function()
					{
						service.blog.setRecommend(blog.id,function(result)
						{
							core.ui.simpleDialog(result.message);
						});
					});
				}
			},
			{
				//删除
				isHandle:function()
				{
					return (module.id==43 || module.id==44);
				},
				handle:function()
				{
					core.ui.simpleDialogEx("是否删除?",function()
					{
						service.blog.remove(blog.id,function(result)
						{
							core.ui.simpleDialog(result.message,function()
							{
								if(result.success)
									util.changeLocation();
							});
						});
					});
				}
			},
			//编辑
			{
				isHandle:function()
				{
					return (module.id==41 || module.id==42);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"编辑博客",
						path:util.getIncludePath("/blog_edit.html"),
						controllerName:"BlogEditController"
					},function($editScope,dialog)
					{
						$editScope.blog=blog;
						
						service.blog.catalog.findByUser(blog.author.id,function(catalogs)
						{
							$editScope.catalogs=catalogs;
							$editScope.$apply();
						});
						
						service.blog.order.find(function(orders)
						{
							$editScope.orders=orders;
							$editScope.$apply();
						});
			
						$editScope.save=function()
						{
							service.blog.edit($editScope.blog,function(result)
							{
								dialog.close();
								if(result.success)
									result.message=core.message.message.UPDATE_SUCCESS;
								else
									result=core.message.parseResult(result);
								core.ui.simpleDialog(result.message,function()
								{
									if(result.success)
										util.refresh();
								});
							});
						};
						
						$editScope.cancel=function()
						{
							dialog.close();
						};
					});
				}
			}
		];
		match(handlerArray);
	};
	
	handler.comment=function(comment,module)
	{
		var handlerArray=
		[
			//编辑
			{
				isHandle:function()
				{
					return (module.id==49 || module.id==50);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"编辑评论",
						path:util.getIncludePath("/comment_edit.html"),
						controllerName:"CommentEditController"
					},function($editScope,dialog)
					{
						$editScope.comment=comment;
			
						$editScope.save=function()
						{
							service.comment.edit($editScope.comment,function(result)
							{
								dialog.close();
								if(result.success)
									result.message=core.message.message.UPDATE_SUCCESS;
								else
									result=core.message.parseResult(parse);
								core.ui.simpleDialog(result.message,function()
								{
									if(result.success)
										util.refresh();
								});
							});
						};
						
						$editScope.cancel=function()
						{
							dialog.close();
						};
					});
				}
			},
			{
				//举报
				isHandle:function()
				{
					return (module.id==57);
				},
				handle:function()
				{
					util.angular.dialog.baseDialog
					({
						title:"举报原因",
						path:util.getIncludePath("/report_create.html"),
						controllerName:"ReportCreateController"
					},function($createScope,dialog)
					{
						$createScope.save=function()
						{
							var report=$createScope.report;
							report.id=comment.id;
							service.comment.report.create(report,function(result)
							{
								dialog.close();
								core.ui.simpleDialog(result.message,function()
								{
									if(result.success)
										util.refresh();
								});
							});	
						}
					});
				}
			},
			{
				//删除
				isHandle:function()
				{
					return (module.id==51 || module.id==52);
				},
				handle:function()
				{
					core.ui.simpleDialogEx("是否删除?",function()
					{
						service.comment.remove(comment.id,function(result)
						{
							core.ui.simpleDialog(result.message,function()
							{
								if(result.success)
									util.refresh();
							});
						});
					});
				}
			}
		];
		match(handlerArray);
	};
	
	//关注
	handler.favorite=
	{
		create:function(func,access,obj,$scope)
		{
			core.ui.simpleDialogEx("是否收藏?",function()
			{
				func(obj.id,function(result)
				{
					core.ui.simpleDialog(result.message);
					if(result.success)
					{
						access.favorite=true;
						obj.favorite_count+=1;
						$scope.$apply();
					}
				});
			});
		},
		remove:function(func,access,obj,$scope)
		{
			core.ui.simpleDialogEx("是否取消收藏?",function()
			{
				func(obj.id,function(result)
				{
					core.ui.simpleDialog(result.message);
					if(result.success)
					{
						access.favorite=false;
						obj.favorite_count-=1;
						$scope.$apply();
					}
				});
			});
		}
	};
	
	return handler;
});